<template>
  <div class="dispatch">
    <editForm
      :formConfig="dispatchConfig"
      ref="dispatchForm"
      :formData="dispatchData"
    >
      <template #teams="{ editFormInfo }">
        <div class="tableConten" v-if="orderType == 'overhaul'">
          <div class="itemTitle">
            <span class="redPoint">*</span>
            工作班组:
          </div>
          <div class="tebleDiv w100">
            <editTableNew
              :tableSetting="userListDetails"
              :hideBtn="hideBtn"
              @handleClickAdd="tableAdd"
              ref="userList"
              :tableData="editFormInfo.teams"
            ></editTableNew>
          </div>
        </div>
      </template>
    </editForm>
    <dialogPublic
      :title="'工作班组'"
      :show.sync="addTeamsDialog"
      @doSubmit="setTeams"
      @doClose="closeDialog"
      appendTobody
    >
      <editForm
        :formConfig="teamsConfig"
        ref="teamsForm"
        :formData="teamsData"
        v-if="addTeamsDialog"
      ></editForm>
    </dialogPublic>
  </div>
</template>
<script>
import {
  getDispatchApi, //公共-获取缺陷-派工信息
  saveDispatchApi, //公共-保存缺陷-派工信息
} from "@/api/orderManagement/common";
import {
  getMaintainDispatchApi, //大修-获取缺陷-派工信息
  saveMaintainDispatchApi, //大修-保存缺陷-派工信息
} from "@/api/orderManagement/overhaul";
export default {
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
    orderType: {
      type: String,
      default: null,
    },
    onlyShow: {
      type: Boolean,
      default: false,
    },
    useType: {
      type: String,
      default: null,
    },
    pageType: {
      type: String,
      default: "",
    },
    needGetData: {
      type: Boolean,
      default: true,
    },
    changeData: {
      type: Object,
      default: () => {
        return {};
      },
    },
    enableConfig: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      joberOrg: "",
      workerOrg: "",
      tempOrgList: [],
      workplace: [],
      /* 派工信息 */
      dispatchConfig: {
        itemArr: [
          {
            /* 缺陷工单、正常维护 */
            type: "select",
            label: "是否外单位",
            prop: "isOut",
            required: true,
            selectOption: [
              {
                name: "是",
                label: "是",
                value: "1",
              },
              {
                name: "否",
                label: "否",
                value: "0",
              },
            ],
            methods: (obj) => {
              this.changeConfig(obj, "change");
            },
            span: 6,
          },
          {
            /* 缺陷工单、正常维护 */
            label: "工作跟踪负责人",
            prop: "trackPerson", //多选时，字段要为数组类型
            propArr: "trackPersonArr",
            idsStr: "trackPersonId",
            orgNameStr: "trackPersonDeptName",
            orgIdStr: "trackPersondept",
            multiple: false, //是否多选
            // isOutOrg: 0, //是否是外单位
            allowCreate: false, //是否允许创建条目
            option: [], //当前选中的项
            type: "user-select", //类型
            trigger: "change",
            methods: (obj) => {
              let data = obj[0];
              let option = [
                {
                  label: data.label,
                  value: data.id || data.value,
                },
              ];
              let notic = [
                {
                  name: data.label,
                  nameBy: data.id || data.value,
                  account: data.id || data.value,
                },
              ];
              this.$refs.dispatchForm.setFormItem(
                "topicWorker",
                JSON.stringify(notic)
              );
              this.$refs.dispatchForm.setFormItem("noticeUserName", data.label);
              this.$refs.dispatchForm.setFormItem(
                "noticeUser",
                data.id || data.value
              );
              setTimeout(() => {
                this.$refs.dispatchForm.setItemOption("noticeUserName", option);
                this.$refs.dispatchForm.setFormItem("noticeArr", [
                  data.id || data.value,
                ]);
              }, 100);
            },
            required: true,
            isHide: true,
            span: 6,
          },
          /* 不是外单位时是本单位的人*/
          {
            /* 缺陷工单、正常维护 */
            label: "工作负责人",
            prop: "headName", //多选时，字段要为数组类型
            propArr: "headArr",
            idsStr: "head",
            orgNameStr: "deptWorkName",
            orgIdStr: "deptWork",
            multiple: false, //是否多选
            isOutOrg: 0, //是否是外单位
            allowCreate: false, //是否允许创建条目
            option: [], //当前选中的项
            type: "user-select", //类型
            trigger: "change",
            methods: (obj, formData) => {
              let { orgName, orgId, isOutOrg } = obj[0];
              if (orgId) {
                this.headOrg = {
                  orderId: this.formData.id,
                  orderType: this.orderType,
                  workDeptName: orgName,
                  workDept: orgId,
                  type: this.$utils.changeUserType(isOutOrg),
                };
                if (orgName == orgId) {
                  this.headOrg.workDept = "";
                }
              } else {
                this.headOrg = {};
              }
              if (this.orderType == "defect") {
                this.workDeptList = [];
                if (orgId) {
                  let option = this.$utils.getOptionData(
                    orgName,
                    orgId,
                    isOutOrg
                  );
                  setTimeout(() => {
                    this.$refs.dispatchForm.setItemOption(
                      "workUnitName",
                      option
                    );
                  }, 100);
                  setTimeout(() => {
                    this.$refs.dispatchForm.setFormItem(
                      "workUnitName",
                      orgName
                    );
                    this.$refs.dispatchForm.setFormItem("workUnit", orgId);
                    this.$refs.dispatchForm.setFormItem("workUnitArr", orgId);
                  }, 300);
                  let org = {
                    orderId: this.formData.id,
                    orderType: this.orderType,
                    workDeptName: orgName,
                    workDept: orgId,
                    type: this.$utils.changeUserType(isOutOrg),
                  };
                  this.workDeptList.push(org);
                } else {
                  this.$refs.dispatchForm.setFormItem("workUnitName", "");
                  this.$refs.dispatchForm.setFormItem("workUnit", "");
                  this.$refs.dispatchForm.setFormItem("workUnitArr", "");
                }
              } else if (
                ["maintain", "experiment", "repair"].includes(this.orderType)
              ) {
                //正常维护\试验工单\计划检修
                setTimeout(() => {
                  //因为选完之后组件里面会修改config.itemArr,所以要确保setOption要其在后面进行
                  this.changeWorkOrg();
                }, 100);
              }
              if (formData.isOut == "0") {
                let data = obj[0];
                if (JSON.stringify(data) != "{}") {
                  let option = [
                    {
                      label: data.label,
                      value: data.id || data.value,
                    },
                  ];
                  let notic = [
                    {
                      name: data.label,
                      nameBy: data.id || data.value,
                      account: data.id || data.value,
                    },
                  ];
                  this.$refs.dispatchForm.setFormItem(
                    "topicWorker",
                    JSON.stringify(notic)
                  );
                  this.$refs.dispatchForm.setFormItem(
                    "noticeUserName",
                    data.label
                  );
                  this.$refs.dispatchForm.setFormItem(
                    "noticeUser",
                    data.id || data.value
                  );
                  setTimeout(() => {
                    this.$refs.dispatchForm.setItemOption(
                      "noticeUserName",
                      option
                    );
                    this.$refs.dispatchForm.setFormItem("noticeArr", [
                      data.id || data.value,
                    ]);
                  }, 100);
                }
              }
            },
            required: true,
            span: 6,
          },
          {
            /* 缺陷工单 */
            label: "作业单位",
            prop: "workUnitName", //多选时，字段要为数组类型
            propArr: "workUnitArr",
            idsStr: "workUnit",
            multiple: false, //是否多选
            isOutOrg: 0, //是否是外单位
            option: [], //this.deptOption1
            allowCreate: true, //是否允许创建条目
            type: "dept-select", //类型
            trigger: "change",
            methods: (obj) => {
              this.workDeptList = [];
              for (let i = 0; i < obj.length; i++) {
                let { name, id, isOutOrg, OutOrg, value, label } = obj[i];
                let org = {
                  orderId: this.formData.id,
                  orderType: this.orderType,
                  workDeptName: label,
                  workDept: value,
                  type: this.$utils.changeUserType(isOutOrg || OutOrg),
                };
                this.workDeptList.push(org);
              }
              this.$set(
                this.dispatchConfig.itemArr[4],
                "defaultOrg",
                obj[0].value
              );
            },
            required: true,
            span: 6,
          },
          {
            /* 缺陷工单、正常维护 */
            label: "工作班成员",
            required: true,
            prop: "workerName", //多选时，字段要为数组类型
            propArr: "workerArr",
            idsStr: "worker",
            orgStr: "workerType",
            orgNameStr: "workerOrgName",
            orgIdStr: "workerOrgId",
            multiple: true, //是否多选
            allowCreate: true, //是否允许创建条目
            option: [], //当前选中的项
            defaultOrg: "",
            type: "user-select", //类型
            trigger: "change",
            methods: (obj) => {
              //缺陷工单\正常维护
              let workerName = null;
              let arr = [];
              let orgArr = [];
              this.workerList = [];
              obj.map((i, index) => {
                let user = {
                  jobDept: i.orgId,
                  jobDeptName: i.orgName,
                  worker: i.id || i.value,
                  workerName: i.name || i.label,
                  type: this.$utils.changeUserType(i.isOutOrg),
                  orderId: this.formData.id,
                  sort: index + 1,
                };
                this.workerList.push(user);
                if (!arr.includes(i.orgName)) {
                  arr.push(i.orgName);
                }
              });
              if (
                ["maintain", "experiment", "repair"].includes(this.orderType)
              ) {
                //正常维护、试验工单、计划检修
                this.workOrgArr = [];
                obj.map((i) => {
                  if (i.orgId) {
                    let org = {
                      orderId: this.formData.id,
                      orderType: this.orderType,
                      workDeptName: i.orgName,
                      workDept: i.orgId,
                      type: this.$utils.changeUserType(i.isOutOrg || i.outOrg),
                    };
                    this.workOrgArr.push(org);
                  }
                });

                setTimeout(() => {
                  this.changeWorkOrg();
                }, 100);
              }
            },
            rows: 2,
            span: 6,
          },
          {
            /* 缺陷工单、正常维护 */
            label: "通知对象",
            prop: "noticeUserName", //多选时，字段要为数组类型
            propArr: "noticeArr",
            idsStr: "noticeUser",
            multiple: true, //是否多选
            isOutOrg: 0,
            allowCreate: false, //是否允许创建条目
            option: [], //当前选中的项
            type: "userOnly-select", //类型
            trigger: "change",
            methods: (data) => {
              let topicWorker = data.map((item) => {
                let obj = {
                  name: item.name,
                  nameBy: item.id,
                  account: item.account,
                };
                return obj;
              });
              this.$refs.dispatchForm.setFormItem(
                "topicWorker",
                JSON.stringify(topicWorker)
              );
            },
            rows: 2,
            span: 6,
          },
          {
            /* 缺陷工单 */
            label: "工作地点",
            prop: "locationName", //多选时，字段要为数组类型
            propArr: "locationArr",
            idsStr: "location",
            orgStr: "locationType", //
            multiple: true, //是否多选
            option: [], //this.deptOption1
            allowCreate: true, //是否允许创建条目
            type: "location-select", //类型
            trigger: "change",
            methods: (obj) => {
              this.workplace = [];
              obj.map((i) => {
                let place = {
                  location: i.isOutOrg == "-1" ? "" : i.value,
                  locationName: i.label,
                  orderId: this.formData.id,
                  orderType: "defect",
                };
                this.workplace.push(place);
              });
            },
            required: true,
            rows: 2,
            span: 6,
          },
          // {
          //   /* 缺陷工单 */
          //   label: "工作地点",
          //   prop: "locationName",
          //   idsStr: "location",
          //   type: "location",
          //   trigger: "change",
          //   required: true,
          //   methods: obj => {},
          //   span: 6
          // },
          {
            /* 缺陷工单 */
            type: "select",
            label: "工单性质",
            prop: "nature",
            required: true,
            selectOption: [
              {
                name: "生产类",
                label: "生产类",
                value: "1",
              },
              {
                name: "非生产类",
                label: "非生产类",
                value: "0",
              },
            ],
            span: 6,
          },
          {
            /* 缺陷工单 */
            label: "计划起止时间",
            prop: "dispatchTime",
            type: "datetimerange",
            required: true,
            pickerOptions: {
              disabledDate:(time)=> {
                return false;
              },
            },
            methods: (obj) => {
              let startTime = new Date(obj[0]);
              let endTime = new Date(obj[1]);
              let timeLimit = this.$utils.getTimeDiffe(startTime, endTime);
              this.$refs.dispatchForm.setFormItem("timeLimit", timeLimit);
            },
            span: 6,
          },
          {
            /* 缺陷工单 */
            label: "工期(天)",
            prop: "timeLimit",
            placeholder: "选完起止时间后自动计算",
            type: "text",
            required: true,
            disabled: true,
            span: 6,
          },
          {
            /* 缺陷工单 */
            label: "占位",
            prop: "placeholder",
            otherInfo: "table",
            isChange: false,
            span: 18,
          },
          {
            /* 正常维护 、试验工单*/
            label: "作业单位",
            prop: "workOrgName", //多选时，字段要为数组类型
            propArr: "workOrgArr",
            idsStr: "workOrg",
            orgStr: "workOrgType", //
            multiple: true, //是否多选
            option: [], //this.deptOption1
            allowCreate: true, //是否允许创建条目
            type: "dept-select", //类型
            trigger: "change",
            methods: (obj) => {
              this.workOrgList = [];
              this.tempOrgList = [];
              obj.map((i) => {
                let org = {
                  orderId: this.formData.id,
                  orderType: this.orderType,
                  workDeptName: i.orgName || i.label,
                  workDept: i.orgId || i.value,
                  type: this.$utils.changeUserType(i.isOutOrg || i.outOrg),
                };
                this.workOrgList.push(org);
                if (i.label == i.value) {
                  this.tempOrgList.push(org);
                }
              });
            },
            required: true,
            rows: 2,
            span: 6,
          },
          {
            /* 缺陷工单 */
            label: "工作内容",
            prop: "context",
            type: "textarea",
            maxlength: "100",
            required: true,
            span: 12,
            rows: 4,
          },
          {
            label: "工作班组",
            prop: "teams",
            otherInfo: "table",
            span: 24,
          },
          {
            /* 缺陷工单\正常维护 */
            label: "注意事项",
            prop: "attention",
            type: "textarea",
            maxlength: "100",
            span: 12,
            rows: 4,
          },
        ],
      },
      teamsConfig: {
        itemArr: [
          {
            type: "select",
            label: "专业",
            prop: "major",
            required: true,
            selectOption: this.$store.getters["dirtData"]("common_major"),
            span: 24,
          },
          {
            label: "现场负责人",
            prop: "joberName", //多选时，字段要为数组类型
            propArr: "joberArr",
            idsStr: "jober",
            orgIdStr: "jobDept",
            orgNameStr: "jobDeptName",
            multiple: false, //是否多选
            isOutOrg: 0, //是否是外单位
            allowCreate: false, //是否允许创建条目
            option: [], //当前选中的项
            type: "user-select", //类型
            trigger: "change",
            methods: (obj) => {
              let { orgName, orgId, isOutOrg } = obj[0];
              this.sceneOrg = {
                orderId: this.formData.id,
                orderType: this.orderType,
                workDeptName: orgName,
                workDept: orgId,
                type: this.$utils.changeUserType(isOutOrg),
              };
              setTimeout(() => {
                this.changeWorkOrg();
              }, 100);
            },
            required: true,
            span: 24,
          },
          {
            label: "工作班成员",
            prop: "worker", //多选时，字段要为数组类型
            propArr: "workerArr",
            idsStr: "workerId",
            orgNameStr: "workerOrgName",
            orgIdStr: "workerOrgId",
            multiple: true, //是否多选
            allowCreate: true, //是否允许创建条目
            option: [], //当前选中的项
            type: "user-select", //类型
            trigger: "change",
            methods: (obj) => {
              this.teamsWorks = [];
              this.workOrgArr = [];
              let workerName = null;
              let arr = [];
              let orgArr = [];
              obj.map((i, index) => {
                let user = {
                  jobDept: i.orgId,
                  jobDeptName: i.orgName,
                  worker: i.id || i.value,
                  workerName: i.name || i.label,
                  type: this.$utils.changeUserType(i.isOutOrg),
                  orderId: this.formData.id,
                  sort: index + 1,
                };
                this.teamsWorks.push(user);
                if (i.orgId) {
                  let org = {
                    orderId: this.formData.id,
                    orderType: this.orderType,
                    workDeptName: i.orgName,
                    workDept: i.orgId,
                    type: this.$utils.changeUserType(i.isOutOrg || i.outOrg),
                  };
                  if (!orgArr.includes(i.orgId)) {
                    this.workOrgArr.push(org);
                    orgArr.push(i.orgId);
                  }
                }
              });
              setTimeout(() => {
                this.changeWorkOrg();
              }, 100);
            },
            rows: 2,
            span: 24,
          },

          {
            /* 大修工单*/
            label: "作业单位",
            prop: "workOrgName", //多选时，字段要为数组类型
            propArr: "workOrgArr",
            idsStr: "workOrg",
            orgStr: "workOrgType", //
            multiple: true, //是否多选
            option: [], //this.deptOption1
            allowCreate: true, //是否允许创建条目
            type: "dept-select", //类型
            trigger: "change",
            methods: (obj) => {
              this.workOrgList = [];
              this.tempOrgList = [];
              let orgArr = [];
              obj.map((i) => {
                let org = {
                  orderId: this.formData.id,
                  orderType: this.orderType,
                  workDeptName: i.orgName || i.label,
                  workDept: i.orgId || i.value,
                  type: this.$utils.changeUserType(i.isOutOrg || i.outOrg),
                };
                if (!orgArr.includes(i.orgId || i.id)) {
                  orgArr.push(i.orgId || i.id);
                  this.workOrgList.push(org);
                  if (i.label == i.value) {
                    this.tempOrgList.push(org);
                  }
                }
              });
            },
            required: true,
            rows: 2,
            span: 24,
          },
        ],
      },
      hideBtn: false,
      addTeamsDialog: false,
      teamsData: {},
      teamsWorks: [],

      userListDetails: {
        title: "",
        select: false,
        addMethons: true,
        maxHeight: 400,
        btnArr: ["add"], //"edit",
        data: [],
        column: [
          {
            label: "专业",
            prop: "major",
            type: "disableText",
            formatter: (val, row) => {
              return this.$store.getters["fineDirtDataName"](
                "common_major",
                row.major
              );
            },
            width: 160,
          },
          {
            label: "现场负责人",
            prop: "joberName",
            id: "jober",
            type: "disableText",
          },
          {
            label: "工作班成员",
            prop: "worker",
            type: "disableText",
            methods: (data, row) => {
              let userArr = [];
              let ordArr = [];
              data.map((i, index) => {
                let form = {
                  jobDept: i.orgId,
                  jobDeptName: i.orgName,
                  worker: i.account,
                  workerName: i.name,
                  userType: i.isOutOrg,
                  orderId: this.formData.id,
                  sort: index + 1,
                };
                if (!ordArr.includes(i.orgName)) {
                  ordArr.push(i.orgName);
                }
                userArr.push(form);
              });
              //设置作业单位数据，以及此条数据的工作班成员数据
              row.workersUnit = ordArr.toString();
              row.workers = userArr;
            },
          },
          {
            label: "作业单位",
            prop: "workersUnit",
            type: "disableText",
          },
        ],
        buttonArr: [
          {
            name: "删除",
            isShow: (row) => {
              return true;
            },
            methods: (row, index) => {
              this.deletRow(row, index);
            },
          },
          {
            name: "编辑",
            isShow: (row) => {
              return true;
            },
            methods: (row, index) => {
              this.addTeamsDialog = true;
              this.tempOrgList = [];
              /* 现场负责人、工作班成员回显 */
              row.rowIndex = index;
              row.joberArr = row.jober;
              let isout = row.jobDept ? "0" : "-1";
              let joberOption = this.$utils.getOptionData(
                row.joberName,
                row.jober,
                isout
              );
              /* 工作班成员 */
              let worker = row.workers || [];
              let userArr = [],
                idsArr = [],
                userType = [],
                orgId = [],
                orgName = [];
              worker.map((j) => {
                userArr.push(j.workerName);
                idsArr.push(j.worker);
                userType.push(j.type);
                if (j.jobDept) {
                  orgId.push(j.jobDept);
                  orgName.push(j.jobDeptName);
                } else {
                  orgId.push("临时人员");
                  orgName.push("临时人员");
                }
              });
              let isOut = this.$utils.changeType(userType);
              row.workerArr = idsArr;
              row.workerOrgName = orgName.toString();
              row.workerOrgId = orgId.toString();
              let workerOption = this.$utils.getOptionData(
                userArr.toString(),
                idsArr.toString(),
                isOut.toString(),
                orgId.toString(),
                orgName.toString()
              ); //本单位人员
              /* 表单作业单位初始化 */
              let workerDeptList = row.workerDept || [];
              let workOrgOption = [];
              if (workerDeptList.length > 0) {
                let deptArr = [],
                  deptId = [],
                  deptType = [];
                workerDeptList.map((i) => {
                  deptArr.push(i.workDeptName);
                  deptId.push(i.workDept);
                  deptType.push(i.type);
                  if (i.workDept == i.workDeptName) {
                    /* 临时单位存储，当工作班成员变化时，作业单位一起变动时能保留输入的临时单位 */
                    let org = {
                      orderId: this.formData.id,
                      orderType: this.orderType,
                      workDeptName: i.workDeptName,
                      workDept: i.workDept,
                      type: i.type,
                    };
                    this.tempOrgList.push(org);
                  }
                });
                deptType = this.$utils.changeType(deptType);
                row.workOrgType = deptType.toString();
                row.workOrg = deptId.toString();
                row.workOrgArr = deptId;
                row.workOrgName = deptArr.toString();
                workOrgOption = this.$utils.getOptionData(
                  deptArr.toString(),
                  deptId.toString(),
                  deptType.toString()
                );
              }

              setTimeout(() => {
                this.$refs.teamsForm.setItemOption("joberName", joberOption); //工作跟踪负责人
                this.$refs.teamsForm.setItemOption("worker", workerOption); //工作班成员
                this.$refs.teamsForm.setItemOption(
                  "workOrgName",
                  workOrgOption
                ); //作业单位
                this.$refs.teamsForm.setFormItem("joberArr", row.jober);
              }, 100);

              setTimeout(() => {
                this.teamsData = row;
              }, 100);
            },
          },
        ],
      },
      dispatchData: {},
      workerList: [], //工作班成员
      workDeptList: [], //作业单位
      headOrg: {}, //工作负责人单位
      sceneOrg: {}, //现场负责人单位（大修）
      workOrgArr: [], //工作班成员单位
      workOrgList: [],
      overhaulStatus: [
        "DRAFT",
        "WAIT_DISPATCH",
        "WAIT_EXECUTE",
        "IN_EXECUTE",
        "PREPARE_REPORT",
        "WAIT_ACCEPT",
        "ARCHIVED",
      ],
    };
  },
  watch: {
    changeData: {
      handler(val) {
        //转换数据
        if (this.orderType == "overhaul") {
          this.transformOverhaulData(val);
        } else {
          this.transformData(val);
        }
      },
      deep: true,
    },
    "formData.status": {
      handler(val, oldVal) {
        //更新数据
        this.initForm();
        if (val != oldVal) {
          this.getDataJudge();
        }
      },
      deep: true,
    },
    pageType: {
      handler(val) {
        if (val == "detail") {
          let itemArr = [];
          //深度拷贝数组对象(可拷贝函数)
          $.extend(true, itemArr, this.dispatchConfig.itemArr);
          (!this.enableConfig) && itemArr.map((i) => (i.disabled = true))
          this.dispatchConfig.itemArr = itemArr;
          this.hideBtn = true;
        }
      },
    },
  },
  computed: {
  },
  created() {
    this.teamsConfig.itemArr[1].allowCreate = true;
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      //是否可编辑
      let { id, status } = this.formData;
      this.initFormConfig();
      if (
        status == "WAIT_DISPATCH" ||
        (this.useType == "orderChange" && !this.onlyShow) //工单变更的时候
      ) {
        //待派工
        //初始化待派工数据;
        this.dispatchData = {
          isOut: "0", //是否是外单位
        };
        this.changeConfig({ value: "0" });
        if (this.orderType == "defect") {
          let startTime = this.$utils.getCurrentDate("minutes");
          let nowDay = new Date().getTime();
          let endTime = new Date(this.formData.expectEndTime).getTime();
          let info = {
            nature: "1",
          };
          if (endTime >= nowDay) {
            // 开始时间小于等于结束时间
            let start = new Date(startTime);
            let end = new Date(this.formData.expectEndTime);
            let timeLimit = this.$utils.getTimeDiffe(start, end);
            info = {
              timeLimit, //工期
              dispatchTime: [startTime, this.formData.expectEndTime], //时间
            };
          }
          this.dispatchData = Object.assign(this.dispatchData, info);
        }
      }
      this.initForm();
      if (this.formData.id) {
        this.getDataJudge();
      }
    },
    //设置工作地点
    setLocation(obj) {
      let data = {};
      let workplace = [obj]; //工作地点
      this.workplace = [obj];
      let placeName = [],
        placeId = [],
        placeType = [];
      workplace.map((i) => {
        placeName.push(i.locationName);
        if (i.location) {
          placeId.push(i.location);
          placeType.push("0");
        } else {
          //临时单位
          placeId.push(i.locationName);
          placeType.push("-1");
        }
      });
      data.locationName = placeName.toString();
      data.locationArr = placeId;
      data.location = placeId.toString();
      data.locationType = placeType.toString();
      //this.dispatchData = Object.assign(this.dispatchData,data);
      let workplaceOption = this.$utils.getOptionData(
        placeName.toString(),
        placeId.toString(),
        placeType.toString()
      );
      this.$refs.dispatchForm.setFormItem("locationName", placeName.toString());
      this.$refs.dispatchForm.setFormItem("locationArr", placeId);
      this.$refs.dispatchForm.setFormItem("location", placeId.toString());
      this.$refs.dispatchForm.setFormItem("locationType", placeType.toString());
      this.$refs.dispatchForm.setItemOption("locationName", workplaceOption);
    },
    initForm() {
      let status = this.formData.status;
      let itemArr = [];
      //深度拷贝数组对象(可拷贝函数)
      $.extend(true, itemArr, this.dispatchConfig.itemArr);
      itemArr.map((i) => {
        if (
          status == "WAIT_DISPATCH" ||
          (this.useType == "orderChange" && !this.onlyShow)
        ) {
          if (["timeLimit"].includes(i.prop)) {
            i.disabled = true;
          } else {
            i.disabled = false;
          }
        } else {
          i.disabled = true;
        }
      });
      this.dispatchConfig.itemArr = itemArr;
      if (
        status == "WAIT_DISPATCH" ||
        (this.useType == "orderChange" && !this.onlyShow)
      ) {
        this.hideBtn = false;
      } else {
        this.hideBtn = true;
      }
    },
    initFormConfig() {
      this.dispatchConfig.itemArr.map((i) => {
        if (this.orderType == "defect") {
          if (
            [
              "isOut",
              "trackPerson",
              "headName",
              "workUnitName",
              "workerName",
              "locationName",
              "dispatchTime",
              "timeLimit",
              "nature",
              "context",
              "attention",
              "placeholder",
              "noticeUserName",
            ].includes(i.prop)
          ) {
            i.isHide = false;
          } else {
            i.isHide = true;
          }
        } else if (
          ["maintain", "experiment", "repair"].includes(this.orderType)
        ) {
          if (
            [
              "isOut",
              "trackPerson",
              "headName",
              "workerName",
              "workOrgName",
              "attention",
              "noticeUserName",
            ].includes(i.prop)
          ) {
            i.isHide = false;
          } else {
            i.isHide = true;
          }
        } else if (this.orderType == "overhaul") {
          if (
            [
              "isOut",
              "trackPerson",
              "headName",
              "teams",
              "attention",
              "noticeUserName",
            ].includes(i.prop)
          ) {
            i.isHide = false;
          } else {
            i.isHide = true;
          }
        }
      });
    },
    getDataJudge() {
      let { id, status } = this.formData;
      //是否需要获取数据
      if (this.needGetData) {
        if (this.orderType == "overhaul") {
          this.getMaintainDispatchDetails();
        } else {
          this.getDispatchDetails();
        }
      } else {
        if (this.orderType == "overhaul") {
          //数据转换
          this.transformOverhaulData(val);
        } else {
          //数据转换
          this.transformData(this.changeData);
        }
      }
    },
    //根据是否是外单位改变界面显示
    changeConfig(obj, type) {
      let itemArr = this.dispatchConfig.itemArr;
      if (this.$refs.dispatchForm && type) {
        /* 清空工作跟踪负责人、工作负责人的数据 */
        this.$refs.dispatchForm.setFormItem("trackPerson", "");
        this.$refs.dispatchForm.setFormItem("trackPersonArr", "");
        this.$refs.dispatchForm.setFormItem("trackPersonId", "");
        this.$refs.dispatchForm.setFormItem("headName", "");
        this.$refs.dispatchForm.setFormItem("headArr", "");
        this.$refs.dispatchForm.setFormItem("head", "");
        this.$refs.dispatchForm.setFormItem("workUnitName", "");
        this.$refs.dispatchForm.setFormItem("workUnit", "");
      }
      for (let i in itemArr) {
        if (itemArr[i].prop == "trackPerson") {
          if (obj.value == 1) {
            //外单位
            itemArr[i].isHide = false;
          } else {
            itemArr[i].isHide = true;
          }
        }
        //设置选人以及选单位的组件是本单位还是外单位
        if (
          itemArr[i].prop == "headName" ||
          itemArr[i].prop == "workUnitName"
        ) {
          //负责人
          itemArr[i].isOutOrg = obj.value || 0;
          if (itemArr[i].prop == "headName") {
            //工作负责人是否可以创建临时人员
            itemArr[i].allowCreate = obj.value == 1 ? true : false;
          }
        }
      }
    },
    //获取派工信息的详情(大修)
    getMaintainDispatchDetails() {
      getMaintainDispatchApi(this.formData.id).then((res) => {
        let data = res.data || {};
        this.transformOverhaulData(data);
      });
    },
    /* 数据转换 */
    transformOverhaulData(data) {
      if (JSON.stringify(data) != "{}") {
        this.changeConfig({ value: data.isOut });
        setTimeout(() => {
          let headOption = [];
          let trackOption = [];
          let workOption = [];
          let { leader, leaderName, outLeader, outLeaderName } = data;
          let noticeList = data.topicWorker ? JSON.parse(data.topicWorker) : [];
          data.noticeUserName = noticeList.map((item) => item.name).join(",");
          data.noticeUser = noticeList.map((item) => item.nameBy).join(",");
          data.noticeArr = noticeList.map((item) => item.nameBy);
          let noticeOption = noticeList.map((item) => {
            let obj = {
              label: item.name,
              value: item.nameBy,
            };
            return obj;
          });
          setTimeout(() => {
            this.$refs.dispatchForm.setItemOption(
              "noticeUserName",
              noticeOption
            );
            //this.$refs.dispatchForm.setFormItem("noticeArr", noticeArr);
          }, 100);
          //是否是外单位
          if (data.isOut == "1") {
            //是外单位
            /* 工作跟踪负责人 */
            data.trackPersonId = leader; //跟踪负责人
            data.trackPerson = leaderName;
            data.trackPersonArr = leader;
            data.head = outLeader; //工作负责人（外单位）
            data.headName = outLeaderName;
            data.headArr = outLeader || outLeaderName;
            trackOption = this.$utils.getOptionData(leaderName, leader, "0"); //本单位人员
            this.$refs.dispatchForm.setItemOption("trackPerson", trackOption); //工作跟踪负责人
            /* 工作负责人 */
            headOption = this.$utils.getOptionData(
              outLeaderName,
              outLeader || outLeaderName,
              outLeader ? "1" : "-1"
            ); //外单位人员
            this.$refs.dispatchForm.setItemOption("headName", headOption); //工作负责人
          } else if (data.isOut == "0") {
            //本单位
            /* 工作负责人 */
            data.head = leader; //工作负责人
            data.headName = leaderName;
            data.headArr = leader;
            headOption = this.$utils.getOptionData(leaderName, leader, "0"); //本单位人员
            this.$refs.dispatchForm.setItemOption("headName", headOption); //工作负责人
          }

          /* 工作班组 */
          let teamsArr = data.teams;
          teamsArr.map((i) => {
            let workerList = i.workers || [];
            let userArr = [];
            let idsArr = [];
            let orgArr = [];
            workerList.map((j) => {
              userArr.push(j.workerName);
              idsArr.push(j.worker);
              if (!orgArr.includes(j.jobDeptName)) {
                //去重
                orgArr.push(j.jobDeptName);
              }
            });
            i.worker = userArr.toString();
            i.workerId = idsArr.toString();
            let workerDeptList = i.workerDept || [];
            if (workerDeptList.length > 0) {
              let deptArr = [];
              workerDeptList.map((i) => {
                deptArr.push(i.workDeptName);
              });
              /* 表格作业单位 */
              i.workersUnit = deptArr.toString();
            }
          });
          setTimeout(() => {
            this.dispatchData = data;
          }, 100);
        }, 100);
      }
    },
    //获取派工信息的详情（缺陷工单；正常维护，计划检修）
    getDispatchDetails() {
      let form = {
        orderId: this.formData.id,
        orderType: this.orderType,
      };
      getDispatchApi(form).then((res) => {
        let data = res.data || {};
        this.transformData(data);
      });
    },
    //转换数据
    transformData(data) {
      if (JSON.stringify(data) != "{}") {
        this.changeConfig({ value: data.isOut });
        setTimeout(() => {
          let headOption = [];
          let trackOption = [];
          let workOption = [];
          let workerOption = []; //
          let workplaceOption = []; //工作地点
          let {
            leader,
            leaderName,
            outLeader,
            outLeaderName,
            deptWork,
            deptWorkName,
            orderWorkDeptPoList, //作业单位
          } = data;
          let noticeList = data.topicWorker ? JSON.parse(data.topicWorker) : [];
          data.noticeUserName = noticeList.map((item) => item.name).join(",");
          data.noticeUser = noticeList.map((item) => item.nameBy).join(",");
          data.noticeArr = noticeList.map((item) => item.nameBy);
          let noticeOption = noticeList.map((item) => {
            let obj = {
              label: item.name,
              value: item.nameBy,
            };
            return obj;
          });
          setTimeout(() => {
            this.$refs.dispatchForm.setItemOption(
              "noticeUserName",
              noticeOption
            );
            //this.$refs.dispatchForm.setFormItem("noticeArr", noticeArr);
          }, 100);
          /* 工作人单位初始化 ，作业单位自动生成时要用到*/
          if (deptWork) {
            this.headOrg = {
              orderId: this.formData.id,
              orderType: this.orderType,
              workDeptName: deptWorkName,
              workDept: deptWork,
              type: this.$utils.changeUserType(data.isOut), //outLeaderType工作负责人的类型
            };
          }
          //是否是外单位

          if (data.isOut == "1") {
            /* deptWork,deptWorkName字段:派工信息是内单位时,填写工作负责人的组织属性,
        外单位时,填写工作跟踪负责人的组织属性 */
            //是外单位
            /* 工作跟踪负责人 */
            data.trackPersondept = deptWork; //工作跟踪负责人单位
            data.trackPersonDeptName = deptWorkName; //工作跟踪负责人单位
            delete data.deptWork;
            delete data.deptWorkName;
            data.trackPersonId = leader; //跟踪负责人
            data.trackPerson = leaderName;
            data.trackPersonArr = leader;
            data.head = outLeader; //工作负责人（外单位）
            data.headName = outLeaderName;
            data.headArr = outLeader || outLeaderName;
            trackOption = this.$utils.getOptionData(leaderName, leader, "0"); //本单位人员
            this.$refs.dispatchForm.setItemOption("trackPerson", trackOption); //工作跟踪负责人
            /* 工作负责人，可以时外单位，也可以时临时输入的人员 */
            headOption = this.$utils.getOptionData(
              outLeaderName,
              outLeader || outLeaderName,
              outLeader ? "1" : "-1"
            ); //外单位人员
            this.$refs.dispatchForm.setItemOption("headName", headOption); //工作负责人
            workOption = this.$utils.getOptionData(deptWorkName, deptWork, "1");
            this.$refs.dispatchForm.setItemOption("deptWorkName", workOption); //作业单位
          } else if (data.isOut == "0") {
            //本单位
            /* 工作负责人,只能时内单位的人 */
            data.head = leader; //工作负责人
            data.headName = leaderName;
            data.headArr = leader;
            headOption = this.$utils.getOptionData(leaderName, leader, "0"); //本单位人员
            this.$refs.dispatchForm.setItemOption("headName", headOption); //工作负责人
            workOption = this.$utils.getOptionData(deptWorkName, deptWork, "0");
            this.$refs.dispatchForm.setItemOption("deptWorkName", workOption); //作业单位
          }

          let name = [],
            idArr = [],
            isOut = [],
            orgId = [],
            orgName = [];
          let workDept = [],
            workDeptName = [],
            workType = []; //作业单位
          orderWorkDeptPoList.map((i) => {
            workDept.push(i.workDept);
            workDeptName.push(i.workDeptName);
            workType.push(i.type);
            //初始化工作班成员的单位，以备作业单位自动生成时使用
            if (i.workDept) {
              let org = {
                orderId: this.formData.id,
                orderType: this.orderType,
                workDeptName: i.workDeptName,
                workDept: i.workDept,
                type: i.type,
              };
              this.workOrgArr.push(org);
              if (i.workDept == i.workDeptName) {
                this.tempOrgList.push(org);
              }
            }
          });

          if (this.orderType == "defect") {
            /* 缺陷工单 */

            //工作地点

            let workplace = data.orderLocationList; //工作地点
            this.workplace = data.orderLocationList;
            let placeName = [],
              placeId = [],
              placeType = [];
            workplace.map((i) => {
              placeName.push(i.locationName);
              if (i.location) {
                placeId.push(i.location);
                placeType.push("0");
              } else {
                //临时单位
                placeId.push(i.locationName);
                placeType.push("-1");
              }
            });
            data.locationName = placeName.toString();
            data.locationArr = placeId;
            data.location = placeId.toString();
            data.locationType = placeType.toString();
            let workplaceOption = this.$utils.getOptionData(
              placeName.toString(),
              placeId.toString(),
              placeType.toString()
            );
            this.$refs.dispatchForm.setItemOption(
              "locationName",
              workplaceOption
            );

            /* 作业单位 */
            this.workDeptList = orderWorkDeptPoList;
            data.workUnitName = workDeptName.toString();
            data.workUnitArr = workDept.toString();
            data.workUnit = workDept.toString();
            workType = this.$utils.changeType(workType);
            let workUnitOption = this.$utils.getOptionData(
              workDeptName.toString(),
              workDept.toString(),
              workType.toString()
            );
            this.$refs.dispatchForm.setItemOption(
              "workUnitName",
              workUnitOption
            );

            data.dispatchTime = [data.startTime || "", data.endTime || ""];
            /* 工期 */
            data.timeLimit = this.$utils.getTimeDiffe(
              new Date(data.startTime),
              new Date(data.endTime)
            );
            let time = {
              startTime: data.startTime,
              endTime: data.endTime,
            };
            /* 更新派工时间，用于执行的时初始化时间 */
            // this.$emit("setDispatchTime", time);
            this.$emit("setDispatchData", data);
            /* 工作班成员 */
            let arr = data.orderDispatchWorkerList || [];
            this.workerList = [].concat(data.orderDispatchWorkerList);
            if (arr.length) {
              arr.map((i) => {
                name.push(i.workerName); //人名
                idArr.push(i.worker);
                isOut.push(i.type);
                orgName.push(i.jobDeptName);
                orgId.push(i.jobDept);
              });
            }
          } else if (
            ["maintain", "experiment", "repair"].includes(this.orderType)
          ) {
            //正常维护\试验工单\计划检修
            /* 工作班成员 */
            let arr = data.orderDispatchWorker || [];
            this.workerList = [].concat(data.orderDispatchWorker);
            arr.map((i) => {
              name.push(i.workerName); //人名
              idArr.push(i.worker);
              isOut.push(i.type);
              orgName.push(i.jobDeptName);
              orgId.push(i.jobDept);
            });
            /* 作业单位 */
            this.workOrgList = [].concat(orderWorkDeptPoList);
            workType = this.$utils.changeType(workType);
            let workOrgOption = this.$utils.getOptionData(
              workDeptName.toString(),
              workDept.toString(),
              workType.toString()
            );
            this.$refs.dispatchForm.setItemOption("workOrgName", workOrgOption);
            data.workOrgArr = workDept;
            data.workOrgName = workDeptName.toString();
            data.workOrg = workDept.toString();
            data.workOrgType = workType.toString();
          }
          isOut = this.$utils.changeType(isOut);
          workerOption = this.$utils.getOptionData(
            name.toString(),
            idArr.toString(),
            isOut.toString(),
            orgId.toString(),
            orgName.toString()
          ); //本单位人员
          this.$refs.dispatchForm.setItemOption("workerName", workerOption); //工作班成员
          data.worker = idArr.toString();
          data.workerName = name.toString();
          data.workerArr = idArr;
          data.workerType = isOut.toString(); //工作班成员类型
          data.workerOrgId = orgId.toString(); //工作班成员单位
          data.workerOrgName = orgName.toString(); //工作班成员单位
          if (this.needGetData) {
            //避免工单变更作业单位，有值时还提示输入
            setTimeout(() => {
              this.dispatchData = data;
            }, 100);
          } else {
            this.dispatchData = data;
          }
        }, 100);
      }
    },
    async saveData(type) {
      let data = await this.$refs.dispatchForm.getFormData();
      /* 工作负责人，工作跟踪负责人 */
      let {
        trackPersonId,
        trackPerson,
        head,
        headName,
        trackPersondept,
        trackPersonDeptName,
      } = data;
      if (data.isOut == "1") {
        /* deptWork,deptWorkName字段:派工信息是内单位时,填写工作负责人的组织属性,
        外单位时,填写工作跟踪负责人的组织属性 */
        //外单位，有跟踪负责人trackPerson/trackPersonId
        data.leader = trackPersonId; //跟踪负责人
        data.leaderName = trackPerson;
        data.outLeader = head; //工作负责人（外单位）
        data.outLeaderName = headName;
        data.deptWork = trackPersondept;
        data.deptWorkName = trackPersonDeptName;
        if (head == headName) {
          data.outLeader = "";
        }
      } else {
        //本单位,
        data.leader = head; //工作负责人
        data.leaderName = headName;
      }
      data.orderId = this.formData.id;
      data.orderType = this.orderType;
      delete data.worker;
      delete data.workerName;
      delete data.workerArr;
      delete data.headArr;
      delete data.noticeUserName;
      delete data.noticeArr;
      delete data.noticeUser;
      delete data.trackPersonArr;
      delete data.workUnitArr; //作业单位
      delete data.locationArr; //工作地点
      delete data.location; //工作地点
      delete data.locationName; //工作地点

      if (this.orderType == "defect") {
        //缺陷工单
        data.startTime = data.dispatchTime[0];
        data.endTime = data.dispatchTime[1];
        data.orderDispatchWorkerList = this.workerList; //工作班成员
        let idArr = [];
        let deptPoList = [];
        /* 作业单位去重 */
        this.workDeptList.map((i) => {
          if (!idArr.includes(i.workDept)) {
            idArr.push(i.workDept);
            deptPoList.push(i);
          }
        });
        data.orderWorkDeptPoList = deptPoList; //作业单位
        data.orderLocationList = this.workplace; //工作地点
      } else if (
        ["maintain", "experiment", "repair"].includes(this.orderType)
      ) {
        //正常维护、试验工单、计划检修
        data.orderDispatchWorker = this.workerList; //工作班成员
        let idArr = [];
        let deptPoList = [];
        this.workOrgList.map((i) => {
          if (!idArr.includes(i.workDept)) {
            idArr.push(i.workDept);
            deptPoList.push(i);
          }
        });

        data.orderWorkDeptPoList = deptPoList; //作业单位
      }
      delete data.dispatchTime;
      delete data.deptWorkNameArr;

      if (this.orderType == "overhaul") {
        //大修工单
        data.teams = (await this.$refs.userList.getTableData()) || [];
        if (data.teams.length > 0) {
          data.teams.map((i, index) => {
            i.sort = index + 1;
          });
          if (type == "save") {
            data.status = this.formData.status;
          } else if (type == "submit") {
            data.status = this.getNextStatus(this.formData.status);
          }
          saveMaintainDispatchApi(data).then((res) => {
            this.$utils.updateStorage();
            this.$message.success("操作成功！");
            if (type == "submit") {
              this.$utils.addOperateLog(this.formData.id, "提交-派工信息");
              this.$emit("changeData");
            } else {
              this.$utils.addOperateLog(this.formData.id, "修改-派工信息");
              this.getMaintainDispatchDetails();
            }
          });
        } else {
          this.$message.warning("请填写作业班组信息！");
        }
      } else {
        if (type == "save") {
          data.isCommit = false;
        } else if (type == "submit") {
          data.isCommit = true;
        }
        saveDispatchApi(data, this.orderType).then((res) => {
          this.$utils.updateStorage();
          this.$message.success("操作成功！");
          if (type == "submit") {
            this.$emit("changeData");
            this.$utils.addOperateLog(this.formData.id, "提交-派工信息");
          } else {
            this.getDispatchDetails();
            this.$utils.addOperateLog(this.formData.id, "修改-派工信息");
          }
        });
      }
    },
    async getDispatchData() {
      let data = await this.$refs.dispatchForm.getFormData();
      /* 工作负责人，工作跟踪负责人 */
      let { trackPersonId, trackPerson, head, headName } = data;
      if (data.isOut == "1") {
        //外单位，有跟踪负责人trackPerson/trackPersonId
        data.leader = trackPersonId; //跟踪负责人
        data.leaderName = trackPerson;
        data.outLeader = head; //工作负责人（外单位）
        data.outLeaderName = headName;
      } else {
        //本单位,
        data.leader = head; //工作负责人
        data.leaderName = headName;
      }
      data.orderId = this.formData.id;
      data.orderType = this.orderType;
      // delete data.worker;
      delete data.workerName;
      delete data.workerArr;
      delete data.headArr;
      delete data.noticeUserName;
      delete data.noticeArr;
      delete data.noticeUser;
      delete data.trackPersonArr;
      delete data.workUnitArr; //作业单位
      delete data.locationArr; //工作地点
      delete data.location; //工作地点
      delete data.locationName; //工作地点
      delete data.locationType; //工作类型

      if (this.orderType == "defect") {
        //缺陷工单
        data.startTime = data.dispatchTime[0];
        data.endTime = data.dispatchTime[1];
        data.orderDispatchWorkerList = this.workerList; //工作班成员
        let idArr = [];
        let deptPoList = [];
        /* 作业单位去重 */
        this.workDeptList.map((i) => {
          if (!idArr.includes(i.workDept)) {
            idArr.push(i.workDept);
            deptPoList.push(i);
          }
        });

        data.orderWorkDeptPoList = deptPoList; //作业单位
        data.orderLocationList = this.workplace; //工作地点
      } else if (
        ["maintain", "experiment", "repair"].includes(this.orderType)
      ) {
        //正常维护、试验工单、计划检修
        data.orderDispatchWorker = this.workerList; //工作班成员
        let idArr = [];
        let deptPoList = [];
        /* 作业单位去重 */
        this.workOrgList.map((i) => {
          if (!idArr.includes(i.workDept)) {
            idArr.push(i.workDept);
            deptPoList.push(i);
          }
        });

        data.orderWorkDeptPoList = deptPoList; //作业单位
      }
      delete data.dispatchTime;
      delete data.deptWorkNameArr;
      if (this.orderType == "overhaul") {
        //大修工单
        data.teams = (await this.$refs.userList.getTableData()) || [];
        if (data.teams.length > 0) {
          data.teams.map((i, index) => {
            i.sort = index + 1;
          });
        } else {
          this.$message.warning("请填写作业班组信息！");
        }
      }
      return data;
    },
    //大修工单获取下一状态
    getNextStatus(status) {
      let arr = this.overhaulStatus;
      let location = arr.indexOf(status);
      return arr[location + 1];
    },
    //删除表格的数据
    async deletRow(row, num) {
      let listArr = [];
      //获取所有填写的执行信息，不进行校验
      let data = await this.$refs.userList.getTableData(false);
      data.map((i, index) => {
        if (index != num) {
          listArr.push(i);
        }
      });
      this.dispatchData.teams = listArr;
      this.$refs.userList.setTableData(listArr);
    },
    //大修工单新增工作班组
    async setTeams() {
      let data = await this.$refs.teamsForm.getFormData();
      data.workers = this.teamsWorks;
      data.workerDept = this.workOrgList; //作业单位
      //表格作业单位显示
      if (this.workOrgList.length > 0) {
        let workerOrg = [];
        data.workerDept.map((i, index) => {
          i.sort = index + 1;
          workerOrg.push(i.workDeptName);
        });
        data.workersUnit = workerOrg.toString();
      }
      let teams = this.dispatchData.teams || []; //工作班成员表格数据
      if (JSON.stringify(this.teamsData) == "{}") {
        //工作班组表格行数据
        //新增data
        data.rowIndex = teams.length;
        teams.push(data);
      } else {
        //编辑
        teams[data.rowIndex] = data;
      }
      this.dispatchData.teams = teams;
      this.$refs.userList.setTableData(teams);
      this.addTeamsDialog = false;
    },
    tableAdd() {
      this.teamsData = {};
      this.addTeamsDialog = true;
    },
    //修改作业单位的数据
    changeWorkOrg() {
      let orgList = JSON.parse(JSON.stringify(this.workOrgArr));
      if (this.orderType == "overhaul") {
        if (this.sceneOrg.workDeptName) {
          /* 去重 */
          let orgArr = [];
          orgList.map((i) => {
            orgArr.push(i.workDept);
          });
          if (!orgArr.includes(this.sceneOrg.workDept)) {
            orgList.push(this.sceneOrg);
          }
        }
      } else {
        if (this.headOrg.workDeptName) {
          orgList.push(this.headOrg);
        }
      }
      //临时输入的作业单位
      if (this.tempOrgList.length > 0) {
        orgList = orgList.concat(this.tempOrgList);
      }
      this.workOrgList = orgList;

      let name = [],
        idArr = [],
        typeArr = [];
      if (orgList.length > 0) {
        orgList.map((i) => {
          if (!idArr.includes(i.workDept)) {
            name.push(i.workDeptName);
            idArr.push(i.workDept);
            typeArr.push(this.$utils.changeUserType(i.type, "code"));
          }
        });
        let option = this.$utils.getOptionData(
          name.toString(),
          idArr.toString(),
          typeArr.toString()
        );
        if (this.orderType == "overhaul") {
          this.$refs.teamsForm.setItemOption("workOrgName", option);
          setTimeout(() => {
            this.$refs.teamsForm.setFormItem("workOrgName", name.toString());
            this.$refs.teamsForm.setFormItem("workOrg", idArr.toString());
            this.$refs.teamsForm.setFormItem("workOrgArr", idArr);
            this.$refs.teamsForm.setFormItem("workOrgType", typeArr.toString());
          }, 200);
        } else {
          this.$refs.dispatchForm.setItemOption("workOrgName", option);
          setTimeout(() => {
            this.$refs.dispatchForm.setFormItem("workOrgName", name.toString());
            this.$refs.dispatchForm.setFormItem("workOrg", idArr.toString());
            this.$refs.dispatchForm.setFormItem("workOrgArr", idArr);
            this.$refs.dispatchForm.setFormItem(
              "workOrgType",
              typeArr.toString()
            );
          }, 200);
        }
      } else {
        if (this.orderType == "overhaul") {
          this.$refs.teamsForm.setFormItem("workOrgName", "");
          this.$refs.teamsForm.setFormItem("workOrg", "");
          this.$refs.teamsForm.setFormItem("workOrgArr", []);
          this.$refs.teamsForm.setFormItem("workOrgType", "");
        } else {
          this.$refs.dispatchForm.setFormItem("workOrgName", "");
          this.$refs.dispatchForm.setFormItem("workOrg", "");
          this.$refs.dispatchForm.setFormItem("workOrgArr", []);
          this.$refs.dispatchForm.setFormItem("workOrgType", "");
        }
      }
    },
    closeDialog() {
      this.addTeamsDialog = false;
      this.workOrgList = [];
      this.workOrgArr = [];
    },
    getFormData() {
      return this.$refs.dispatchForm.getCurrentData();
    },
  },
};
</script>
<style lang="less" scoped>
.dispatch {
  /deep/.selectContent .el-input--suffix .el-input__inner {
    min-height: 32px !important;
  }
}
.tableConten {
  display: flex;
  flex-flow: wrap nowrap;
  margin-top: 10px;
  .redPoint {
    color: red;
  }
  .itemTitle {
    width: 120px;
  }
  .tebleDiv-bottom {
    margin-top: 10px;
  }
}
</style>
